package com.cloudlead.customerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.TbCustomerOrderService;
import com.cloudlead.commonData.service.TbFleetOrderService;
import com.cloudlead.commonData.vo.TbCustomerOrder;
import com.cloudlead.commonData.vo.TbFleetOrder;
import com.cloudlead.customerAction.service.TbFleetOrderFindSer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by 20150817 on 2015/8/31.
 */
@Service
public class TbFleetOrderFindSerImpl implements TbFleetOrderFindSer{
    @Autowired//必须要的
    TbFleetOrderService tbFleetOrderService;
    @Autowired
    TbCustomerOrderService tbCustomerOrderService;

    //根据条件查询
    @Override
    public Object tbFleetOrderFind(String customerOrderId, String isStart) {
        //创建一个too对象
        TbFleetOrder too = new TbFleetOrder();
        //创建一个tco对象
        TbCustomerOrder tco = new TbCustomerOrder();
        tco.setId(customerOrderId);//把输入的值赋给tco
        too.setIsStart(isStart);//把输入的值赋给too
        too.setTbCustomerOrder(tco);//把tco赋给too
        too.setFlag("0");//Flag为0
        //返回查询
        TbFleetOrder tfo = tbFleetOrderService.findOne(too);
        if(tfo.getTbCustomerOrder()!=null&&tfo.getTbCustomerOrder().getId()!=null){//订单id不为空
            TbCustomerOrder tcr= tbCustomerOrderService.view(tfo.getTbCustomerOrder().getId());//查出订单信息
            tfo.setTbCustomerOrder(tcr);//替换tfo订单信息
        }
        return tfo;
    }

    @Override
    public Page<Object> tbFleetOrderFindAll(String companyId, String isStart,int size,int page) {
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        tr.add(new Term("flag","0"));//未删除
        //查询条件
        if(companyId!=null&&(!"".equals(companyId))) {
            tr.add(new Term("fleetCompanyId",companyId));//公司ID
        }
        if(isStart!=null&&(!"".equals(isStart))) {
            tr.add(new Term("isStart",isStart));//是否起始港车队
        }
        Filter filter=new Filter( new Criteria( tr));
            pr.setPage(page);//页码
            pr.setSize(size);//每页数量
            pr.setFilter(filter);
        Sort sort = new Sort();//订单创建时间倒序
            sort.add(new Sort.Order(Sort.Direction.DESC, "createTime"));
            pr.setSort(sort);
        Page<TbFleetOrder> px =tbFleetOrderService.findAll(pr);
        Page<Object> po = new Page<Object>();
        List<TbFleetOrder> lx=px.getContent();
        List<Object> lx1 = new ArrayList<Object>();
        for(Iterator<TbFleetOrder> it = lx.iterator();it.hasNext();) {//遍历
            TbFleetOrder fo = it.next();
            if(fo.getTbCustomerOrder()!=null&&fo.getTbCustomerOrder().getId()!=null){//订单id不为空
                TbCustomerOrder tcr= tbCustomerOrderService.view(fo.getTbCustomerOrder().getId());//查出订单信息
                if(fo!=null)
                    fo.setTbCustomerOrder(tcr);//替换tfo订单信息
            }
            lx1.add(fo);
        }
        BeanUtils.copyProperties(px, po);//拷贝page
        po.setContent(lx1);
        return po;
    }

    //车队订单回执
    @Override
    public String tbFleetOrderStatus(String id, String orderStatus) {
        TbFleetOrder tbFleetOrder = tbFleetOrderService.view(id);//根据ID查询
        if(tbFleetOrder!=null&&!tbFleetOrder.equals("")) {
            tbFleetOrder.setOrderState(orderStatus);//设置订单状态
            tbFleetOrderService.update(tbFleetOrder);
        }else {
            return "error ID";
        }
        return "success";
    }
}
